package com.tarena.lbs.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;

import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.mapper.UserTagsMapper;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.dao.repository.UserTagsRepository;
import com.tarena.lbs.user.pojo.param.UserParam;
import com.tarena.lbs.user.pojo.param.UserTagsParam;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.po.UserTagsPO;
import com.tarena.lbs.user.pojo.vo.UserVO;
import com.tarena.lbs.user.service.UserService;
import com.tarena.lbs.user.utils.RandomUserName;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
   UserRepository  userRepository;
    private AuthenticationContextUtils AuthenticationContextUtil;
    @Autowired
    private UserTagsRepository userTagsRepository;



    @Override
    public List<UserVO> list(UserParam param) {
        log.info("用户列表查询参数：{}",param);
        List<UserPO> list = userRepository.listUser(param);
        List<UserVO> listVO = list.stream().map(po -> {
            UserVO vo = new UserVO();
            BeanUtils.copyProperties(po, vo);
            return vo;
        }).collect(Collectors.toList());
        return listVO;
    }

    @Override
    public void register(UserParam userParam) throws BusinessException {
//        1.保证注册的用户 手机号不重复注册
        checkPhone(userParam.getPhone());
        //2.新增创建user表格的一条数据
        saveUser(userParam);
    }


    private void saveUser(UserParam userParam) {
        //param转化成po 补充缺少属性保证新增insert成功
        UserPO po=new UserPO();
        BeanUtils.copyProperties(userParam,po);//phone password
        //1.状态 status初始化0 刚刚注册
        po.setStatus(0);
        //2.nickname username 用户暂时没有填写 使用一个随机名称
        String randomName = RandomUserName.createRandomName();
        po.setNickName(randomName);
        po.setUserName(randomName);
        //3.入住时间
        po.setRegTime(new Date());
        userRepository.save(po);
    }
    private void checkPhone(String phone) throws BusinessException {
        //select count(*) from lbs_user where phone=?
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("phone",phone);
        Long count = userRepository.count(queryWrapper);
        Asserts.isTrue(count>0,new BusinessException("-2","手机号重复"));
    }
    @Override
    public UserVO detail(Integer userId) throws BusinessException {
//        Integer id=getUserId();
        UserPO po = userRepository.getById(userId);
        Asserts.isTrue(po==null,new BusinessException("-2","登录用户根本不存在"));

        UserVO vo=new UserVO();
        BeanUtils.copyProperties(po,vo);
        return vo;
    }

    @Override
    public void bindUserTags(UserTagsParam param) {
        log.info("用户标签关联参数：{}",param);
        UserTagsPO po=new UserTagsPO();
        BeanUtils.copyProperties(param,po);
        userTagsRepository.insert(po);
    }

//    private Integer getUserId() throws BusinessException {
//        UserPrinciple userPrinciple = AuthenticationContextUtil.get();
//        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","用户认证解析失败"));
//        return userPrinciple.getId();
//    }



}
